package com.genersoft.iot.vmp.storager.dao;

import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
import com.genersoft.iot.vmp.vmanager.gb28181.platform.bean.ChannelReduce;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface PlatformChannelMapper {

    /**
     * 查询列表里已经关联的
     */
    @Select("<script> "+
            "SELECT device_and_channel_id FROM platform_gb_channel WHERE platform_id='${platformId}' AND device_and_channel_id in" +
            "<foreach collection='deviceAndChannelIds' open='(' item='id_' separator=',' close=')'> '${id_}'</foreach> ORDER BY device_and_channel_id ASC" +
            "</script>")
    List<String> findChannelRelatedPlatform(String platformId, List<String> deviceAndChannelIds);

    @Insert("<script> "+
            "INSERT INTO platform_gb_channel (channel_id, device_id, platform_id, device_and_channel_id) VALUES" +
            "<foreach collection='channelReducesToAdd'  item='item' separator=','> ('${item.channelId}','${item.deviceId}', '${platformId}', '${item.deviceId}_${item.channelId}' )</foreach>" +
            "</script>")
    int addChannels(String platformId, List<ChannelReduce> channelReducesToAdd);


    @Delete("<script> "+
            "DELETE FROM platform_gb_channel WHERE platform_id='${platformId}' AND device_and_channel_id in" +
            "<foreach collection='channelReducesToDel'  item='item'  open='(' separator=',' close=')' > '${item.deviceId}_${item.channelId}'</foreach>" +
            "</script>")
    int delChannelForGB(String platformId, List<ChannelReduce> channelReducesToDel);

    @Delete("<script> "+
            "DELETE FROM platform_gb_channel WHERE device_id='${deviceId}' " +
            "</script>")
    int delChannelForDeviceId(String deviceId);

    @Delete("<script> "+
            "DELETE FROM platform_gb_channel WHERE platform_id='${platformId}'"  +
            "</script>")
    int cleanChannelForGB(String platformId);


    @Select("SELECT * FROM device_channel WHERE device_id = (SELECT device_id FROM platform_gb_channel WHERE " +
            "platform_id='${platformId}' AND channel_id='${channelId}' ) AND channel_id='${channelId}'")
    @Results(id="pdc",value={
   		 @Result(property = "channelId", column = "channel_id"),@Result(property = "deviceId", column = "device_id"),
   		 @Result(property = "name", column = "name"),@Result(property = "manufacture", column = "manufacture"),
   		 @Result(property = "model", column = "model"),@Result(property = "owner", column = "owner"),
   		 @Result(property = "civilCode", column = "civil_code"),@Result(property = "block", column = "block"),
   		 @Result(property = "address", column = "address"),@Result(property = "parental", column = "parental"),
   		 @Result(property = "parentId", column = "parent_id"),@Result(property = "safetyWay", column = "safety_way"),
   		 @Result(property = "registerWay", column = "register_way"),@Result(property = "certNum", column = "cert_num"),
   		 @Result(property = "certifiable", column = "certifiable"),@Result(property = "errCode", column = "err_code"),
   		 @Result(property = "endTime", column = "end_time"),@Result(property = "secrecy", column = "secrecy"),
   		 @Result(property = "ipAddress", column = "ip_address"),@Result(property = "port", column = "port"),
   		 @Result(property = "password", column = "password"),@Result(property = "PTZType", column = "ptz_type"),
   		 @Result(property = "status", column = "status"),@Result(property = "createTime", column = "create_time"),
   		 @Result(property = "updateTime", column = "update_time"),@Result(property = "longitude", column = "longitude"),
   		 @Result(property = "latitude", column = "latitude"),@Result(property = "streamId", column = "stream_id"),
   		 @Result(property = "hasAudio", column = "has_audio"),@Result(property = "subCount", column = "subCount"),
   		
   })
    DeviceChannel queryChannelInParentPlatform(String platformId, String channelId);

    @Select("SELECT * FROM device WHERE device_id = (SELECT device_id FROM platform_gb_channel WHERE platform_id='${platformId}' AND channel_id='${channelId}')")
    @Results(id="pdevice",
    value = { 
       @Result(property = "deviceId", column = "device_id"),
       @Result(property = "name", column = "name"), 
       @Result(property = "manufacturer", column = "manufacturer"),
       @Result(property = "model", column = "model"),
       @Result(property = "firmware", column = "firmware"), 
       @Result(property = "transport", column = "transport"), 
       @Result(property = "streamMode", column = "stream_mode"), 
       @Result(property = "ip", column = "ip"), 
       @Result(property = "port", column = "port"), 
       @Result(property = "hostAddress", column = "host_address"), 
       @Result(property = "expires", column = "expires"), 
       @Result(property = "registerTime", column = "register_time"), 
       @Result(property = "keepaliveTime", column = "keepalive_time"), 
       @Result(property = "createTime", column = "create_time"), 
       @Result(property = "updateTime", column = "update_time"), 
       @Result(property = "online", column = "onlines"), 
       })
    Device queryVideoDeviceByPlatformIdAndChannelId(String platformId, String channelId);
}
